package tech.lq0.dreamaticvoyage.network.packet;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;
import tech.lq0.dreamaticvoyage.network.ClientPacketHandler;

import java.util.List;
import java.util.function.Supplier;

/**
 * Code based on @GoryMoon's Chargers
 */
public class ContainerComponentDataSyncPacket {

    private final int containerId;
    private final List<Pair> data;

    public ContainerComponentDataSyncPacket(int containerId, List<Pair> data) {
        this.containerId = containerId;
        this.data = data;
    }

    public static ContainerComponentDataSyncPacket decode(FriendlyByteBuf buf) {
        return new ContainerComponentDataSyncPacket(buf.readUnsignedByte(), buf.readList(byteBuf -> new Pair(byteBuf.readShort(), byteBuf.readComponent())));
    }

    public static void encode(ContainerComponentDataSyncPacket message, FriendlyByteBuf buf) {
        buf.writeByte(message.containerId);
        buf.writeCollection(message.data, (byteBuf, p) -> p.write(byteBuf));
    }

    public static void handle(ContainerComponentDataSyncPacket message, Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> DistExecutor.unsafeRunWhenOn(Dist.CLIENT,
                () -> () -> ClientPacketHandler.handleContainerComponentDataSyncPacket(message.containerId, message.data, ctx)));
        ctx.get().setPacketHandled(true);
    }

    public static class Pair {

        public int id;
        public Component data;

        public Pair(int id, Component data) {
            this.id = id;
            this.data = data;
        }

        public void write(FriendlyByteBuf buf) {
            buf.writeShort(id);
            buf.writeComponent(data);
        }
    }
}
